/**
 * Copyright(c) 2010 Ceno Techonologies Co., Ltd.
 *
 * History:
 *   15-6-3 下午1:57 Created by lyyang
 */
package com.xunton.bss.weixin.api.message.event;

import java.io.Serializable;
import java.io.StringReader;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

import com.xunton.bss.weixin.api.adapter.SecondToDateAdapter;

/**
 * 接受微信推送消息事件
 *
 * @author <a href="mailto:lyyang@ceno.cn">lyyang</a>
 * @version 1.0 15-6-3 下午1:57
 */
@MappedSuperclass
@XmlAccessorType (XmlAccessType.FIELD)
public abstract class Event implements Serializable
{
    @Column(name="to_user_name")
    @XmlElement(name = "ToUserName")
    private String toUserName;

    @Column(name="from_user_name")
    @XmlElement(name = "FromUserName")
    private String fromUserName;

    @Column(name="create_time")
    @XmlElement(name = "CreateTime")
    @XmlJavaTypeAdapter (SecondToDateAdapter.class)
    private Date createTime;

    @Column(name="msg_type")
    @XmlElement(name = "MsgType")
    private String msgType;

    @Column(name="event")
    @XmlElement(name = "Event")
    private String event;



    public abstract String getType();

    public String getToUserName()
    {
        return toUserName;
    }

    public void setToUserName(String toUserName)
    {
        this.toUserName = toUserName;
    }

    public String getFromUserName()
    {
        return fromUserName;
    }

    public void setFromUserName(String fromUserName)
    {
        this.fromUserName = fromUserName;
    }


    public Date getCreateTime()
    {
        return createTime;
    }


    public void setCreateTime(Date createTime)
    {
        this.createTime = createTime;
    }

    public String getMsgType()
    {
        return msgType;
    }

    public void setMsgType(String msgType)
    {
        this.msgType = msgType;
    }

    public String getEvent()
    {
        return event;
    }

    public void setEvent(String event)
    {
        this.event = event;
    }



    public static void main(String[] args){
        try{
            String xml = "<xml><ToUserName><![CDATA[gh_344333f49bae]]></ToUserName><FromUserName><![CDATA[oCi76t5vtZttO57dyX7q1P3_5S6Q]]></FromUserName><CreateTime>1433312829</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[1]]></Content><MsgId>6156031725698535195</MsgId></xml>";
            JAXBContext context = JAXBContext.newInstance(Event.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            StringReader reader = new StringReader(xml);
            Event event = (Event)unmarshaller.unmarshal(reader);

        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }
}
